<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>JavaCore实践-时钟显示 | 小谭的部落阁</title><meta name="keywords" content="实践,Java,时钟"><meta name="author" content="谭自财"><meta name="copyright" content="谭自财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="目标界面 实现代码import java.awt.*;import java.util.Date;import javax.swing.*;public class Clock extends JComponent&amp;#123;    private static final long serialVersionUID &#x3D; -5379472973578609775L;    private Font">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaCore实践-时钟显示">
<meta property="og:url" content="https://tanzicai.gitee.io/2021/03/21/JavaCore%E5%AE%9E%E8%B7%B5-%E6%97%B6%E9%92%9F%E6%98%BE%E7%A4%BA/index.html">
<meta property="og:site_name" content="小谭的部落阁">
<meta property="og:description" content="目标界面 实现代码import java.awt.*;import java.util.Date;import javax.swing.*;public class Clock extends JComponent&amp;#123;    private static final long serialVersionUID &#x3D; -5379472973578609775L;    private Font">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tanzicai.gitee.io/img/survivors_v02_5120x2880.png">
<meta property="article:published_time" content="2021-03-20T23:14:33.000Z">
<meta property="article:modified_time" content="2021-07-11T09:06:40.000Z">
<meta property="article:author" content="谭自财">
<meta property="article:tag" content="实践">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="时钟">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tanzicai.gitee.io/img/survivors_v02_5120x2880.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://tanzicai.gitee.io/2021/03/21/JavaCore%E5%AE%9E%E8%B7%B5-%E6%97%B6%E9%92%9F%E6%98%BE%E7%A4%BA/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JavaCore实践-时钟显示',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-07-11 17:06:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="小谭的部落阁" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/survivors_v02_5120x2880.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小谭的部落阁</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JavaCore实践-时钟显示</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-03-20T23:14:33.000Z" title="发表于 2021-03-21 07:14:33">2021-03-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-07-11T09:06:40.000Z" title="更新于 2021-07-11 17:06:40">2021-07-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JavaCore实践-时钟显示"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="目标界面"><a href="#目标界面" class="headerlink" title="目标界面"></a>目标界面</h2><p><img src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210321071629.png" alt="image-20210321071629582"></p>
<h2 id="实现代码"><a href="#实现代码" class="headerlink" title="实现代码"></a>实现代码</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Clock</span> <span class="keyword">extends</span> <span class="title">JComponent</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = -<span class="number">5379472973578609775L</span>;</span><br><span class="line">    <span class="keyword">private</span> Font f = <span class="keyword">new</span> Font(<span class="string">&quot;微软雅黑&quot;</span>,Font.PLAIN,<span class="number">15</span>);</span><br><span class="line">    <span class="keyword">private</span> Font f2 = <span class="keyword">new</span> Font(<span class="string">&quot;微软雅黑&quot;</span>,Font.BOLD,<span class="number">15</span>);</span><br><span class="line">    <span class="keyword">private</span> JLabel l = <span class="keyword">new</span> JLabel(<span class="string">&quot;当前时间：&quot;</span>);</span><br><span class="line">    <span class="keyword">private</span> JLabel display = <span class="keyword">new</span> JLabel();</span><br><span class="line">    <span class="keyword">private</span> JLabel display2 = <span class="keyword">new</span> JLabel(<span class="string">&quot;&quot;</span>);</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> hour = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> min = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> sec = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">private</span> Date now = <span class="keyword">new</span> Date();</span><br><span class="line">    <span class="keyword">private</span> Graphics2D g;</span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">double</span> PI = Math.PI;</span><br><span class="line">    <span class="keyword">private</span> String strTime = <span class="string">&quot;&quot;</span> ;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@SuppressWarnings(&quot;deprecation&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Clock</span><span class="params">()</span></span>&#123;</span><br><span class="line">        add(l);</span><br><span class="line">        l.setBounds(<span class="number">120</span>, <span class="number">320</span>, <span class="number">80</span>, <span class="number">20</span>);</span><br><span class="line">        l.setFont(f);</span><br><span class="line">        add(display);</span><br><span class="line">        display.setBounds(<span class="number">195</span>, <span class="number">320</span>, <span class="number">80</span>, <span class="number">20</span>);</span><br><span class="line">        display.setFont(f);</span><br><span class="line">        display.setBorder(BorderFactory.createLineBorder(Color.black));</span><br><span class="line">        add(display2);</span><br><span class="line">        display2.setBounds(<span class="number">90</span>, <span class="number">350</span>, <span class="number">250</span>, <span class="number">20</span>);</span><br><span class="line">        display2.setFont(f);</span><br><span class="line">        hour = now.getHours();</span><br><span class="line">        min = now.getMinutes();</span><br><span class="line">        sec = now.getSeconds();</span><br><span class="line">        setVisible(<span class="keyword">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">paintComponent</span><span class="params">(Graphics g1)</span></span>&#123;</span><br><span class="line">        <span class="keyword">double</span> x,y;</span><br><span class="line">        <span class="keyword">super</span>.paintComponent(g1);</span><br><span class="line">        g = (Graphics2D) g1;</span><br><span class="line">        <span class="comment">//反锯齿开关开</span></span><br><span class="line">        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//画表盘</span></span><br><span class="line">        g.setPaint(<span class="keyword">new</span> GradientPaint(<span class="number">5</span>,<span class="number">40</span>,Color.blue,<span class="number">15</span>,<span class="number">50</span>,Color.yellow,<span class="keyword">true</span>));</span><br><span class="line">        g.setStroke( <span class="keyword">new</span> BasicStroke(<span class="number">3</span>,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));</span><br><span class="line">        g.drawOval(<span class="number">75</span>, <span class="number">40</span>, <span class="number">250</span>, <span class="number">250</span>);</span><br><span class="line">        g.fillOval(<span class="number">195</span>, <span class="number">160</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">        g.setColor(Color.black);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//画60个点</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; <span class="number">60</span>;i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">double</span>[] co = <span class="keyword">new</span> <span class="keyword">double</span>[<span class="number">2</span>];</span><br><span class="line">            co = paint_Dot(i * <span class="number">2</span> * PI / <span class="number">60</span>);</span><br><span class="line">            x = co[<span class="number">0</span>];</span><br><span class="line">            y = co[<span class="number">1</span>];</span><br><span class="line">            <span class="keyword">if</span>(i == <span class="number">0</span> || i == <span class="number">15</span> || i == <span class="number">30</span> || i == <span class="number">45</span>)<span class="comment">//画3,6,9,12四个大点</span></span><br><span class="line">            &#123;</span><br><span class="line">                g.fillOval((<span class="keyword">int</span>)(x - <span class="number">5</span> + <span class="number">200</span>),(<span class="keyword">int</span>)(y - <span class="number">5</span> + <span class="number">165</span>),<span class="number">10</span>,<span class="number">10</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span><span class="comment">//其他小点</span></span><br><span class="line">            &#123;</span><br><span class="line">                g.fillOval((<span class="keyword">int</span>)(x - <span class="number">2.5</span> + <span class="number">200</span>),(<span class="keyword">int</span>)(y - <span class="number">2.5</span> + <span class="number">165</span>),<span class="number">5</span>,<span class="number">5</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//画四个数字</span></span><br><span class="line">        g.setFont(f2);</span><br><span class="line">        g.drawString(<span class="string">&quot;3&quot;</span>, <span class="number">300</span>, <span class="number">171</span>);</span><br><span class="line">        g.drawString(<span class="string">&quot;6&quot;</span>, <span class="number">195</span>, <span class="number">273</span>);</span><br><span class="line">        g.drawString(<span class="string">&quot;9&quot;</span>, <span class="number">91</span>, <span class="number">171</span>);</span><br><span class="line">        g.drawString(<span class="string">&quot;12&quot;</span>, <span class="number">190</span>, <span class="number">68</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//画时针，分针，秒针</span></span><br><span class="line">        paint_HourPointer(hour*<span class="number">3600</span> + min*<span class="number">60</span> + sec,g);<span class="comment">//时针走过的秒数</span></span><br><span class="line">        paint_MinutePointer(min*<span class="number">60</span> + sec,g);<span class="comment">//分针走过的秒数</span></span><br><span class="line">        paint_SecondPointer(sec,g);<span class="comment">//秒针走过的秒数</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showUI</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Thread() &#123;</span><br><span class="line">            <span class="meta">@SuppressWarnings(&quot;deprecation&quot;)</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                <span class="keyword">while</span> (<span class="keyword">true</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    now = <span class="keyword">new</span> Date();</span><br><span class="line">                    hour = now.getHours();</span><br><span class="line">                    min = now.getMinutes();</span><br><span class="line">                    sec = now.getSeconds();</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (InterruptedException ex) &#123;</span><br><span class="line">                        ex.printStackTrace();</span><br><span class="line">                    &#125;</span><br><span class="line">                    showTime();</span><br><span class="line">                    repaint();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;.start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">paint_HourPointer</span><span class="params">(<span class="keyword">int</span> second,Graphics2D g)</span></span>&#123;<span class="comment">//second表示当前时间的时针相对00:00:00走了多少秒</span></span><br><span class="line">        <span class="keyword">double</span> x,y,angle;</span><br><span class="line">        angle = second * PI / <span class="number">21600</span>;<span class="comment">//时针的速度为PI/21600 (rad/s)</span></span><br><span class="line">        x = <span class="number">200</span> + <span class="number">60</span> * Math.sin(angle);</span><br><span class="line">        y = <span class="number">165</span> - <span class="number">60</span> * Math.cos(angle);</span><br><span class="line">        g.setStroke( <span class="keyword">new</span> BasicStroke(<span class="number">5</span>,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));</span><br><span class="line">        g.setPaint(<span class="keyword">new</span> GradientPaint(<span class="number">200</span>,<span class="number">165</span>,Color.red,<span class="number">260</span>,<span class="number">165</span>,Color.blue,<span class="keyword">true</span>));</span><br><span class="line">        g.drawLine(<span class="number">200</span>, <span class="number">165</span>, (<span class="keyword">int</span>)x, (<span class="keyword">int</span>)y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">paint_MinutePointer</span><span class="params">(<span class="keyword">int</span> second,Graphics2D g)</span></span>&#123;<span class="comment">//second表示当前时间的分针相对00:00:00走了多少秒</span></span><br><span class="line">        <span class="keyword">double</span> x,y,angle;</span><br><span class="line">        angle = second * PI / <span class="number">1800</span>;<span class="comment">//分针的速度为PI/1800 (rad/s)</span></span><br><span class="line">        x = <span class="number">200</span> + <span class="number">80</span> * Math.sin(angle);</span><br><span class="line">        y = <span class="number">165</span> - <span class="number">80</span> * Math.cos(angle);</span><br><span class="line">        g.setStroke( <span class="keyword">new</span> BasicStroke(<span class="number">3</span>,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));</span><br><span class="line">        g.setPaint(<span class="keyword">new</span> GradientPaint(<span class="number">200</span>,<span class="number">165</span>,Color.magenta,<span class="number">280</span>,<span class="number">165</span>,Color.blue,<span class="keyword">true</span>));</span><br><span class="line">        g.drawLine(<span class="number">200</span>, <span class="number">165</span>, (<span class="keyword">int</span>)x, (<span class="keyword">int</span>)y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">paint_SecondPointer</span><span class="params">(<span class="keyword">int</span> second,Graphics2D g)</span></span>&#123;<span class="comment">//second表示当前时间的秒针相对00:00:00走了多少秒</span></span><br><span class="line">        <span class="keyword">double</span> x,y,x1,y1,x2,y2,x3,y3,angle;</span><br><span class="line">        <span class="keyword">double</span> cos = <span class="number">90</span> / Math.sqrt(<span class="number">8125</span>);<span class="comment">//90*90+5*5</span></span><br><span class="line">        <span class="keyword">double</span> sin = <span class="number">5</span> / Math.sqrt(<span class="number">8125</span>);</span><br><span class="line">        angle = second * PI / <span class="number">30</span>;<span class="comment">//时针的速度为PI/30 (rad/s)</span></span><br><span class="line">        x = <span class="number">200</span> + <span class="number">95</span> * Math.sin(angle);</span><br><span class="line">        y = <span class="number">165</span> - <span class="number">95</span> * Math.cos(angle);</span><br><span class="line">        x1 = <span class="number">200</span> + <span class="number">20</span> * Math.sin(angle + PI);</span><br><span class="line">        y1 = <span class="number">165</span> - <span class="number">20</span> * Math.cos(angle + PI);</span><br><span class="line">        x2 = <span class="number">200</span> + Math.sqrt(<span class="number">8125</span>)* ( Math.sin(angle)*cos - Math.cos(angle)*sin ); <span class="comment">//sin(a-b)</span></span><br><span class="line">        y2 = <span class="number">165</span> - Math.sqrt(<span class="number">8125</span>)* ( Math.cos(angle)*cos + Math.sin(angle)*sin ); <span class="comment">//cos(a-b)</span></span><br><span class="line">        x3 = <span class="number">200</span> + Math.sqrt(<span class="number">8125</span>)* ( Math.sin(angle)*cos + Math.cos(angle)*sin ); <span class="comment">//sin(a+b)</span></span><br><span class="line">        y3 = <span class="number">165</span> - Math.sqrt(<span class="number">8125</span>)* ( Math.cos(angle)*cos - Math.sin(angle)*sin ); <span class="comment">//cos(a+b)</span></span><br><span class="line">        g.setStroke( <span class="keyword">new</span> BasicStroke(<span class="number">2</span>,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));</span><br><span class="line">        g.setPaint(<span class="keyword">new</span> GradientPaint(<span class="number">180</span>,<span class="number">165</span>,Color.CYAN,<span class="number">295</span>,<span class="number">165</span>,Color.MAGENTA,<span class="keyword">true</span>));</span><br><span class="line">        g.drawLine((<span class="keyword">int</span>)x1, (<span class="keyword">int</span>)y1, (<span class="keyword">int</span>)x, (<span class="keyword">int</span>)y);</span><br><span class="line">        g.drawLine((<span class="keyword">int</span>)x2, (<span class="keyword">int</span>)y2, (<span class="keyword">int</span>)x, (<span class="keyword">int</span>)y);</span><br><span class="line">        g.drawLine((<span class="keyword">int</span>)x3, (<span class="keyword">int</span>)y3, (<span class="keyword">int</span>)x, (<span class="keyword">int</span>)y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">double</span>[] paint_Dot(<span class="keyword">double</span> angle)&#123;</span><br><span class="line">        <span class="keyword">double</span>[] co = <span class="keyword">new</span> <span class="keyword">double</span>[<span class="number">2</span>];</span><br><span class="line">        co[<span class="number">0</span>] = <span class="number">115</span> * Math.cos(angle);<span class="comment">//横坐标</span></span><br><span class="line">        co[<span class="number">1</span>] = <span class="number">115</span> * Math.sin(angle);<span class="comment">//纵坐标</span></span><br><span class="line">        <span class="keyword">return</span> co;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@SuppressWarnings(&quot;deprecation&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">showTime</span><span class="params">()</span></span>&#123;</span><br><span class="line">        String date;</span><br><span class="line">        <span class="keyword">int</span> hour_temp = hour,min_temp = min,sec_temp = sec;</span><br><span class="line">        sec_temp += <span class="number">1</span> ;</span><br><span class="line">        <span class="keyword">if</span>(sec_temp &gt;= <span class="number">60</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            sec_temp = <span class="number">0</span>;</span><br><span class="line">            min_temp += <span class="number">1</span> ;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(min_temp&gt;=<span class="number">60</span>)&#123;</span><br><span class="line">            min_temp=<span class="number">0</span>;</span><br><span class="line">            hour_temp+=<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(hour_temp &lt; <span class="number">10</span>)</span><br><span class="line">            strTime = <span class="string">&quot;0&quot;</span> + hour_temp + <span class="string">&quot;:&quot;</span>;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            strTime = <span class="string">&quot;&quot;</span> + hour_temp + <span class="string">&quot;:&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(min_temp &lt; <span class="number">10</span>)</span><br><span class="line">            strTime = strTime + <span class="string">&quot;0&quot;</span> + min_temp + <span class="string">&quot;:&quot;</span>;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            strTime = strTime + <span class="string">&quot;&quot;</span> + min_temp + <span class="string">&quot;:&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(sec &lt; <span class="number">10</span>)</span><br><span class="line">            strTime = strTime + <span class="string">&quot;0&quot;</span> + sec_temp;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            strTime = strTime + <span class="string">&quot;&quot;</span> + sec_temp;</span><br><span class="line">        <span class="comment">//在窗体上设置显示时间</span></span><br><span class="line">        date = <span class="string">&quot; &quot;</span> + (now.getYear()+<span class="number">1900</span>) + <span class="string">&quot;年&quot;</span> + (now.getMonth()+<span class="number">1</span>) + <span class="string">&quot;月&quot;</span> + now.getDate() + <span class="string">&quot;日   &quot;</span> + <span class="string">&quot;星期&quot;</span> ;</span><br><span class="line">        <span class="keyword">switch</span> (now.getDay()) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">            date += <span class="string">&quot;一&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">            date += <span class="string">&quot;二&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">            date += <span class="string">&quot;三&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">            date += <span class="string">&quot;四&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">            date += <span class="string">&quot;五&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">            date += <span class="string">&quot;六&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">            date += <span class="string">&quot;日&quot;</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        date += <span class="string">&quot;  CST&quot;</span>;</span><br><span class="line">        strTime = <span class="string">&quot;  &quot;</span> + strTime;</span><br><span class="line">        display.setText(strTime);</span><br><span class="line">        display2.setText(date);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        Clock c = <span class="keyword">new</span> Clock();</span><br><span class="line">        c.showUI();</span><br><span class="line">        JFrame f = <span class="keyword">new</span> JFrame(<span class="string">&quot;Clock By XJX&quot;</span>);</span><br><span class="line">        Image img=Toolkit.getDefaultToolkit().getImage(<span class="string">&quot;title.gif&quot;</span>);<span class="comment">//窗口图标</span></span><br><span class="line">        f.setIconImage(img);</span><br><span class="line">        f.setSize(<span class="number">400</span>,<span class="number">400</span>);</span><br><span class="line">        f.setResizable(<span class="keyword">false</span>);</span><br><span class="line">        f.add(c, BorderLayout.CENTER);</span><br><span class="line">        f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">        f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">谭自财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tanzicai.gitee.io/2021/03/21/JavaCore%E5%AE%9E%E8%B7%B5-%E6%97%B6%E9%92%9F%E6%98%BE%E7%A4%BA/">https://tanzicai.gitee.io/2021/03/21/JavaCore实践-时钟显示/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tanzicai.gitee.io" target="_blank">小谭的部落阁</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%AE%9E%E8%B7%B5/">实践</a><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/%E6%97%B6%E9%92%9F/">时钟</a></div><div class="post_share"><div class="social-share" data-image="/img/survivors_v02_5120x2880.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/03/21/Tex%E8%A1%8C%E5%86%85%E5%85%AC%E5%BC%8F%E8%AF%B4%E6%98%8E/"><img class="prev-cover" src="/img/GRSS.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Tex行内公式说明</div></div></a></div><div class="next-post pull-right"><a href="/2021/03/21/Java%E5%AD%A6%E4%B9%A0%E8%B7%AF%E7%BA%BF/"><img class="next-cover" src="/img/CTTN.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java学习路线</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/03/21/Java学习路线/" title="Java学习路线"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">Java学习路线</div></div></a></div><div><a href="/2022/02/09/JAVA基础语法/" title="Java知识"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java知识</div></div></a></div><div><a href="/2022/02/10/常用类/" title="Java常用类"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-10</div><div class="title">Java常用类</div></div></a></div><div><a href="/2022/02/09/异常/" title="Java异常"><img class="cover" src="/img/survivors_v03_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java异常</div></div></a></div><div><a href="/2022/02/09/面向对象编程/" title="Java面向对象编程"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java面向对象编程</div></div></a></div><div><a href="/2022/02/09/数组详解/" title="Java数组详解"><img class="cover" src="/img/CNYN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java数组详解</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">谭自财</div><div class="author-info__description">你别卷入世俗，我别沉迷初遇</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/tanzicai"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/tanzicai" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://gitee.com/tanzicai" target="_blank" title="Github"><i class="fab fa-git-square"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=1728561744&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fas fa-qq"></i></a><a class="social-icon" href="mailto:1728561744@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">本站正在切换主题测试，如有错误提示，请联系我，谢谢</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%AE%E6%A0%87%E7%95%8C%E9%9D%A2"><span class="toc-number">1.</span> <span class="toc-text">目标界面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E4%BB%A3%E7%A0%81"><span class="toc-number">2.</span> <span class="toc-text">实现代码</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类"><img src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java常用类"/></a><div class="content"><a class="title" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类">Java常用类</a><time datetime="2022-02-10T12:25:00.000Z" title="发表于 2022-02-10 20:25:00">2022-02-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常"><img src="/img/survivors_v03_5120x2880.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java异常"/></a><div class="content"><a class="title" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常">Java异常</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程"><img src="/img/CTTN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java面向对象编程"/></a><div class="content"><a class="title" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程">Java面向对象编程</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解"><img src="/img/CNYN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java数组详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解">Java数组详解</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解"><img src="/img/WHT.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java方法详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解">Java方法详解</a><time datetime="2022-02-09T12:25:00.000Z" title="发表于 2022-02-09 20:25:00">2022-02-09</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/img/survivors_v02_5120x2880.png')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 谭自财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">欢迎进入 <a href="https://tanzicai.gitee.io">小谭的部落阁</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>